//
//  CellForChinaJunqing.m
//  PCAForWar
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CellForChinaJunqing.h"
#import "JunQingCell.h"
#import "ModelForChinaJunQIng.h"
#import <MJRefresh.h>
#import <DKNightVersion.h>
#import "NetWorkingHandler.h"

@interface CellForChinaJunqing () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) NSString *strForShangla;
@property (nonatomic, assign) NSInteger pageForShangLa;
@property (nonatomic, retain) UIButton *buttonTop;

@end

@implementation CellForChinaJunqing
- (void)dealloc{
    
    _tableViewJunQing.dataSource = nil;
    _tableViewJunQing.delegate = nil;
    
    [_tableViewJunQing release];
    [_arrForJunQing release];
    [_strForShangla release];
    [_buttonTop release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.arrForJunQing = [NSMutableArray array];
        
        self.strForShangla = @"page=1&type=2&processID=getNewsList&pageSize=20&";
        
        self.pageForShangLa = 1;
        
        [self handleData];
        [self createTableView];
        [self createButtonForTop];
        [self header];
        [self shangLa];
        [self Night];
    }
    return self;
}

#pragma mark 夜间模式
- (void)Night {
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    
    self.tableViewJunQing.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
}

#pragma mark  解析数据
- (void)handleData {
    
    [NetWorkingHandler handleDataByStr:@"http://admin.militaryapp.china.com/News/NewsServlet.do" body:self.strForShangla header:@"application/x-www-form-urlencoded; charset=UTF-8"  block:^(id result) {
        
        NSArray *arrDown = [result objectForKey:@"data"];
        
        // 判断  如果没有数据了  不会crash
        if ([arrDown.class isSubclassOfClass:[NSMutableArray class]]) {
            
            for (NSDictionary *dic in arrDown) {
                
                ModelForChinaJunQIng *model = [[ModelForChinaJunQIng alloc] init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [self.arrForJunQing addObject:model];
                
                [self.tableViewJunQing reloadData];
                
            }
            
        }
        
        [self.tableViewJunQing.mj_footer endRefreshing];
        
    }];
    
}

#pragma mark 添加tableView
- (void)createTableView {
    
    self.tableViewJunQing = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
    
    [self.contentView addSubview:self.tableViewJunQing];
    
    self.tableViewJunQing.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableViewJunQing.showsVerticalScrollIndicator = NO;
    
    self.tableViewJunQing.dataSource = self;
    self.tableViewJunQing.delegate = self;
    
    [self.tableViewJunQing registerClass:[JunQingCell class] forCellReuseIdentifier:@"pool1"];
    
    [_tableViewJunQing release];
}

#pragma mark tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrForJunQing.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 判断出现TopButton
    if (indexPath.row == 30) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"showButton2" object:nil];
    }
    if (indexPath.row == 15) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"hiddenButton2" object:nil];
    }
    
    JunQingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    ModelForChinaJunQIng *model = [self.arrForJunQing objectAtIndex:indexPath.row];
    
    [cell passModel:model];
    
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:52 / 256.f green:52 / 256.f blue:52 / 256.f alpha:1]);
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
    
}

#pragma mark 下拉刷新
- (void)header {
    
    self.tableViewJunQing.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewAction)];
    
    [self.tableViewJunQing.mj_header beginRefreshing];
    
}

- (void)loadNewAction {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.tableViewJunQing.mj_header endRefreshing];
        
        [self.tableViewJunQing reloadData];
        
    });
}

#pragma mark 上拉刷新
- (void)shangLa {
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.strForShangla = [NSString stringWithFormat:@"page=%ld&type=2&processID=getNewsList&pageSize=20&", ++self.pageForShangLa];
        
        [self handleData];
        
    }];
    
    self.tableViewJunQing.mj_footer = footer;
}

#pragma mark 创建Top button
- (void)createButtonForTop {
    
    self.buttonTop = [UIButton buttonWithType:UIButtonTypeSystem];
    
    self.buttonTop.frame = CGRectMake(self.tableViewJunQing.frame.size.width - 60, self.tableViewJunQing.frame.size.width + 20, 30, 30);
    
    [self.contentView addSubview:self.buttonTop];
    
    [self.buttonTop setImage:[UIImage imageNamed:@"Dingbu"] forState:UIControlStateNormal];
    
    self.buttonTop.hidden = YES;
    
    [self.buttonTop addTarget:self action:@selector(TopAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 通知判断出现
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleShowButton) name:@"showButton2" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleButtonclick) name:@"hiddenButton2" object:nil];
}

- (void)handleButtonclick {
    
    self.buttonTop.hidden = YES;
    
}
- (void)handleShowButton {
    
    self.buttonTop.hidden = NO;
    
}

- (void)TopAction:(UIButton *)button {
    self.buttonTop.hidden = YES;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    
    [self.tableViewJunQing scrollToRowAtIndexPath:indexPath atScrollPosition: UITableViewScrollPositionTop animated:YES];
    
}

@end
